package club.terroir.client.produit;

import java.io.Serializable;

import club.terroir.client.producteur.AchatCreateGUI;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;



public class Produit implements Serializable,IFolder{

	
	private static final long serialVersionUID = 1L;
	public Folder parent;
	public String  id;
	public String name ;
	
	public static  interface SelectProduitListener {
		public void select(Produit produiSelected);
		public void removeAchat(Widget produiSelectedWidget);
	}
	public Produit() {
	}
	public Produit(String nameNewProduit, Folder folder) {
		this();
		this.name = nameNewProduit;
		this.parent = folder;
	}

	@Override
	public TreeItem createTreeItemModification() {
		// TODO Auto-generated method stub
		TreeItem treeItem = new TreeItem(getWidget());
		return treeItem;
	}

	private Widget getWidget() {
			final Label label =  new Label( "---- "+this.name);
			label.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				final PopupPanel popup = new PopupPanel(true, true);
				Panel vPanel = new VerticalPanel();
				final TextBox textField = new TextBox();
				Button buttonEnregistrer = new Button("Enregistrer");
				buttonEnregistrer.addClickHandler(new ClickHandler() {					
					@Override
					public void onClick(ClickEvent event) {
						String nameNew = textField.getText();
						name = nameNew;
						label.setText(nameNew);
						FolderFactory.instance.enregistrerProduit(Produit.this);
						popup.hide();
					}
				});
				Button buttonDelete= new Button("Supprimer");
				buttonDelete.addClickHandler(new ClickHandler() {
					
					@Override
					public void onClick(ClickEvent event) {
						if (parent != null){
							parent.childreen.remove(Produit.this);
							label.setVisible(false);
							popup.hide();
						}
					}
				});
				Button buttonCANCEL = new Button("Anuller");
				buttonCANCEL.addClickHandler(new ClickHandler() {					
					@Override
					public void onClick(ClickEvent event) {
						popup.hide();
					}
				});
				
				
				textField.setText(name);
				vPanel.add(textField);
				vPanel.add(buttonEnregistrer);
				vPanel.add(buttonDelete);
				vPanel.add(buttonCANCEL);
				popup.add(vPanel);
				popup.setSize("200", "200");
				popup.center();
				popup.show();
			}
		});
		return label;
	}
	
	
	@Override
	public TreeItem createTreeItemSelection() {
		Label label = new Label("x--- "+this.name);
		label.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
					AchatCreateGUI.instance.select(Produit.this);						
			}
		});
		return new TreeItem(label) ;
	}
	
	public Widget getPanelAchat() {
		final HorizontalPanel panelAchat =  new HorizontalPanel();
		Button buttonRemove = new Button("Supprimer");
		buttonRemove.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				AchatCreateGUI.instance.removeAchat(panelAchat);
			}
		});
		
		panelAchat.add( buttonRemove);
		
		panelAchat.add( new Label(this.name));
		;
		return panelAchat;
	}

}
